﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Controller;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


namespace QLKS
{
    public partial class FrmInfoCustomer : Form
    {
        Controller_CheckOut cont_chOut = new Controller_CheckOut();
        Controller_CheckIn cont_ChIn = new Controller_CheckIn();
        public FrmInfoCustomer()
        {
            InitializeComponent();
        }

        private void FrmInfoCustomer_Load(object sender, EventArgs e)
        {
            dgv_Customer.DataSource = cont_chOut.Select_InfoCustomer().Tables[0];
            cmb_Search.SelectedIndex = 1;
            cmb_Month.SelectedIndex = 0;
            cmb_Year.Text = DateTime.Now.Year.ToString();
        }

        public void Load_DgvCus()
        {
            dgv_Customer.DataSource = cont_chOut.Select_InfoCustomer().Tables[0];
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            doSearch();
        }
        public void doSearch()
        {
            
            DataSet ds = cont_chOut.Select_InfoCustomer();
            DataView dv = ds.Tables[0].DefaultView;

            if (txt_Search.Text.ToString().Trim() =="")
            {
                dgv_Customer.DataSource = dv;
                return;
            }
            if (cmb_Search.SelectedIndex == 0)
            {
                dv.RowFilter = @"[Mã HĐ] like '%" + txt_Search.Text.ToString()+ "%'";
            }
            else if (cmb_Search.SelectedIndex == 1)
                dv.RowFilter = @"[Tên Khách Hàng] like '%" + txt_Search.Text.ToString().TrimEnd() + "%'";
            else if (cmb_Search.SelectedIndex == 2)
                dv.RowFilter = @"[CMND] like '%" + txt_Search.Text + "%'";
            else if (cmb_Search.SelectedIndex == 3)
                dv.RowFilter = @"[Tên phòng] like '%" + txt_Search.Text + "%'";
            dgv_Customer.DataSource = dv;
        }

        private void txt_Search_KeyPress(object sender, KeyPressEventArgs e)
        {
           //if (cmb_Search.SelectedIndex == 0)
           // {
           //     if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
           //         e.Handled = true;
           // }
        }

        private void txt_Search_TextChanged(object sender, EventArgs e)
        {
            doSearch();
        }

        private void btnHoaDon_Click(object sender, EventArgs e)
        {

            doShowReport();
        }

        private void doShowReport()
        {

            foreach (Form f in MdiChildren)
            {
                if (f is frmHoaDon2)
                {
                    f.Activate();
                    return;
                }
            }
            int r = dgv_Customer.CurrentRow.Index;
            string maKH = dgv_Customer.Rows[r].Cells[0].Value.ToString();
            DataSet ds = new DataSet();
            ds = cont_chOut.show_HoaDon(maKH);

            frmHoaDon2 obj = new frmHoaDon2();
            rptHoaDon rptHD2 = new rptHoaDon();

            if (ds.Tables.Count != 0)
                rptHD2.SetDataSource(ds.Tables[0]);

            decimal giaTien = decimal.Parse(ds.Tables[0].Rows[0]["Tổng tiền"].ToString());
            string thanhTien = ReadNumber(giaTien);
            ((TextObject)rptHD2.ReportDefinition.ReportObjects["txtTongTien"]).Text = thanhTien;
            obj.rptView.ReportSource = rptHD2;
            obj.MdiParent = ParentForm as Frm_Main;
            obj.Show();
            
            
        }

        private void dgv_Customer_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            doShowReport();
        }

        

        public static string ReadNumber(decimal number)
        {
            string str = "";
            string s = number.ToString("#");
            string[] so = new string[] { "Không", "Một", "Hai", "Ba", "Bốn", "Năm", "Sáu", "Bảy", "Tám", "Chín" };
            string[] hang = new string[] { "", "nghìn", "triệu", "tỷ" };
            int i, j, donvi, chuc, tram;

            bool booAm = false;
            decimal decS = 0;
            //Tung addnew
            try
            {
                decS = Convert.ToDecimal(s.ToString());
            }
            catch
            {
            }
            if (decS < 0)
            {
                decS = -decS;
                s = decS.ToString();
                booAm = true;
            }
            i = s.Length;
            if (i == 0)
                str = so[0] + str;
            else
            {
                j = 0;
                while (i > 0)
                {
                    donvi = Convert.ToInt32(s.Substring(i - 1, 1));
                    i--;
                    if (i > 0)
                        chuc = Convert.ToInt32(s.Substring(i - 1, 1));
                    else
                        chuc = -1;
                    i--;
                    if (i > 0)
                        tram = Convert.ToInt32(s.Substring(i - 1, 1));
                    else
                        tram = -1;
                    i--;
                    if ((donvi > 0) || (chuc > 0) || (tram > 0) || (j == 3))
                        str = hang[j] + str;
                    j++;
                    if (j > 3) j = 1;
                    if ((donvi == 1) && (chuc > 1))
                        str = "một " + str;
                    else
                    {
                        if ((donvi == 5) && (chuc > 0))
                            str = "lăm " + str;
                        else if (donvi > 0)
                            str = so[donvi] + " " + str;
                    }
                    if (chuc < 0)
                        break;
                    else
                    {
                        if ((chuc == 0) && (donvi > 0)) str = "lẻ " + str;
                        if (chuc == 1) str = "mười " + str;
                        if (chuc > 1) str = so[chuc] + " mươi " + str;
                    }
                    if (tram < 0) break;
                    else
                    {
                        if ((tram > 0) || (chuc > 0) || (donvi > 0)) str = so[tram] + " trăm " + str;
                    }
                    str = " " + str;
                }
            }
            if (booAm) str = "Âm " + str;
            return str = str + "đồng";

        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds = cont_chOut.Select_InfoCustomer();
            DataView dv = ds.Tables[0].DefaultView;
            if (cmb_Month.SelectedIndex == 0)
            {
                dgv_Customer.DataSource = dv;
            }
            else
            {
                string filter = @"[Ngày trả] like '%/"+cmb_Month.Text+"/"+cmb_Year.Text+"%'";
                dv.RowFilter = filter;
                dgv_Customer.DataSource = dv;
            }
        }

        private void btnSuaThongtin_Click(object sender, EventArgs e)
        {
            int r = dgv_Customer.CurrentRow.Index;
            string maKH = dgv_Customer.Rows[r].Cells[0].Value.ToString();
            DataSet ds = cont_ChIn.Select_CheckInInfo(maKH);
            string DetailsID = null;
            string CheckInDay = null, CheckOutDay = null;
            string AmountRoom = null, NguoiLon = null, TreEm = null;
            string Deposit = null, Amount = null;
            if (ds.Tables.Count != 0)
            {
                DetailsID = ds.Tables[0].Rows[0]["DetailsID"].ToString().TrimEnd();
                CheckInDay = ds.Tables[0].Rows[0]["CheckInDay"].ToString();
                CheckOutDay = ds.Tables[0].Rows[0]["CheckOutDay"].ToString();
                AmountRoom = (ds.Tables[0].Rows[0]["AmountRoom"].ToString()=="") ? "0":ds.Tables[0].Rows[0]["AmountRoom"].ToString();
                NguoiLon = (ds.Tables[0].Rows[0]["NguoiLon"].ToString() == "") ? "0" : ds.Tables[0].Rows[0]["NguoiLon"].ToString();
                TreEm = (ds.Tables[0].Rows[0]["TreEm"].ToString() == "") ? "0" : ds.Tables[0].Rows[0]["TreEm"].ToString();
                Deposit = ds.Tables[0].Rows[0]["Deposit"].ToString();
                Amount = ds.Tables[0].Rows[0]["Amount"].ToString();
            }
            else
                MessageBox.Show("Không chọn được thông tin khách hàng","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            frmCheckInUpdate obj = new frmCheckInUpdate(DetailsID, CheckInDay, CheckOutDay, AmountRoom, NguoiLon, TreEm, Deposit, Amount,this);
            obj.MdiParent = ParentForm as Frm_Main;
            obj.Show();
        }




    }
}
